package halberd.filter;

import halberd.DataType;
import halberd.ImgFilter;
import halberd.data.ImgData;
import halberd.data.ImgDataFactory;

/**
 * 底片
 * 
 * @author jiang
 *
 */
public class NegativeFilter implements ImgFilter {

	public ImgData process(ImgData input) {
		if (!canProcess(input)) {
			return null;
		}
		ImgData result = ImgDataFactory.createImgData(input.getWidth(), input.getHeight());
		negative(input.getDatas(), result.getDatas(), input.getWidth(), input.getHeight());
		result.setDataType(input.getDataType());
		return result;
	}

	private boolean canProcess(ImgData input) {
		if (input == null) {
			return false;
		}
		DataType dt = input.getDataType();
		if (dt == DataType.RGB) {
			return true;
		}
		if (dt == DataType.RGBA) {
			return true;
		}
		return false;
	}

	private void negative(int[][][] src, int[][][] dst, int w, int h) {
		for (int i = 0; i < w; ++i) {
			for (int j = 0; j < h; ++j) {
				dst[i][j][0] =  (255 - src[i][j][0]);
				dst[i][j][1] =  (255 - src[i][j][1]);
				dst[i][j][2] =  (255 - src[i][j][2]);
				dst[i][j][3] = src[i][j][3];
			}
		}
	}
}
